<template>
  <div>
      <!-- 添加或删除传感器类型 -->
       <el-dialog :title="formTitle" :visible.sync="dialogFormVisible" width="40%">
        <el-form :model="saveForm">
            <el-form-item label="传感器类型名" prop="ldid" label-width="20%">
                <el-input v-model="saveForm.name" style="width:85%"></el-input>
            </el-form-item>
            <el-form-item label="传感器类型单位" prop="name" label-width="20%">
                <el-input v-model="saveForm.unit" style="width:85%"></el-input>
            </el-form-item>
            <el-form-item label="传感器类型描述" label-width="20%">
                <el-input v-model="saveForm.desc" style="width:85%"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="editDialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="updateEdit()">确 定</el-button>
        </div>
      </el-dialog>
      <!-- 查询区域 -->
      <el-form :inline="true" :model="queryForm" class="demo-form-inline" style="border:1px solid #DCDFE6;padding: 10px;">
        <el-form-item label="类型名">
            <el-input v-model="queryForm.name" placeholder="请输入传感器类型名"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="onSubmit()">查询</el-button>
        </el-form-item>
       </el-form>
       <!-- 显示区域 -->
       <el-container style="border:1px solid #DCDFE6;padding: 10px;margin-top:10px">
           <el-table
            :data="sensorTypes"
            stripe
            style="width: 100%">
            <el-table-column
            type="index"
            width="70">
            </el-table-column>
            <el-table-column
            label="传感器类型名"
            width="253">
            <template slot-scope="scope">
                <span >{{ scope.row.name }}</span>
            </template>
            </el-table-column>
            <el-table-column
            label="传感器类型单位"
            width="220">
            <template slot-scope="scope">
                 <span>{{scope.row.unit}}</span>
            </template>
            </el-table-column>
             <el-table-column
            label="传感器类型描述"
            width="500">
            <template slot-scope="scope">
                 <span>{{scope.row.desc}}</span>
            </template>
            </el-table-column>
            <el-table-column label="操作"  width="230">
                <template slot="header">
                    <el-button
                    size="medium"
                    type="primary"
                    plain
                    @click="handleAdd()">添加传感器类型</el-button>
                </template>
                <template slot-scope="scope">
                    <el-button
                    size="mini"
                    @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                   <el-popover
                    placement="top"
                    width="160"
                    trigger="click"
                    v-model="scope.row.visible">
                    <p>您确定要删除吗？</p>
                    <div style="text-align: right; margin: 0">
                        <el-button size="mini" type="text" @click="scope.row.visible = false">取消</el-button>
                        <el-button type="primary" size="mini" @click="handleDelete(scope.$index, scope.row)">确定</el-button>
                    </div>
                    <el-button 
                    style="margin-left:20px"
                    slot="reference"
                    size="mini"
                    type="danger">删除</el-button>
                    </el-popover>
                </template>
            </el-table-column>
        </el-table>
       </el-container>
       <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryForm.current"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="queryForm.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="dataSize">
        </el-pagination>
  </div>
</template>

<script>
import Qs from 'qs'
export default {
    data(){
        return{
            dialogFormVisible:false,//弹出框是否显示
            formTitle:"",//弹出框标题
            dataSize:400,// 数据总数
            queryForm:{
                name:"",
                current:1,
                size:10
            },
            saveForm:{},
            sensorTypes:[{
                id:1,
                name:"温度传感器",
                unit:"℃",
                desc:"这是一个温度传感器",
                },{
                id:2,
                name:"湿度传感器",
                unit:"hPa",
                desc:"这是一个湿度传感器",
                }
            ],
        }
    },
    created(){
        this.onSubmit();
    },
    methods:{
        //添加角色
        handleAdd(){
            this.dialogFormVisible = true;
            this.formTitle = "添加传感器";
            this.saveForm = {};
        },
        //提交查询
        async onSubmit() {
            const res = await this.$http.get("/sensortype/listSensorTypessByPage",{params:this.queryForm})
             if(res.data.code == 200){
                 this.sensorTypes=res.data.data.records;
                 this.dataSize =res.data.data.total;
             }else{
                 this.$message.error(res.data.msg);
             }
        },
        // 编辑按钮
        handleEdit(index,row){
            this.dialogFormVisible = true;
            this.formTitle = "修改传感器";
            this.saveForm = Object.assign({}, row);
        },
        // 提交修改或保存
        async updateEdit(){
            const res = await this.$http.post("/sensortype/saveSensorType",Qs.stringify(this.saveForm))
            if(res.data.code === 200){
                 this.$message({
                    message: res.data.msg,
                    type: 'success'
                });
                this.dialogFormVisible = false;
                // 刷新当前页面
                this.onSubmit();
            }else{
                 this.$message.error(res.data.msg);
            }
        },
        // 删除按钮
        async handleDelete(index,row){
             row.visible = false;
            // 通过删除按钮根据id删除传感器类型
            const res = await this.$http.post("/sensortype/deleteSensorType",Qs.stringify({id:row.id}))
            if(res.data.code === 200){
                this.$message({
                    message: res.data.msg,
                    type: 'success'
                });
                this.onSubmit();
            }else{
                this.$message.error(res.data.msg);
            }
        },
        // 每叶总数改变
        handleSizeChange(val) {
            this.queryForm.size = val;
            this.onSubmit();
        },
        //当前页
        handleCurrentChange(val) {
            this.queryForm.current =val;
            this.onSubmit();
        }
        
    }
}
</script>

<style>

</style>